#!/usr/bin/perl 

use strict;
use DBI;
use CGI;
# use TTrakerSite;
use TTraker;

my $dbh;
initialize_dbi;

print_header;

my $form_ref = &parse_input;
my %FORM_DATA = %$form_ref;

my $supNum = $FORM_DATA{'supNum'};
my $supName = $FORM_DATA{'supName'};
my $supJob = $FORM_DATA{'supJob'};
my $supLoc = $FORM_DATA{'supLoc'};
my $empNum = $FORM_DATA{'empNum'};
my $payDate = $FORM_DATA{'payDate'};
my @jbs=();

# check that the employee # exists

my $head_count = get_employee_count ($empNum);

if ( $head_count == 1 ) { # this is good - one match found in employees table
	my $array_ref = get_employee_info ($empNum);
	my ($num, $first, $last, $job, $loc, $fac) = @$array_ref;
	my $empName = "$first" . " $last";
	my $job_ref = get_task_job_list ($empNum, $payDate);
	my @jLst = @$job_ref;
	my $jCount = $#jLst;
	if ( $jCount == -1 ) {
		my $msg = "No Jobs Found for # $empNum $empName on $payDate";
		redraw_task_job_screen ($supNum, $supName, $supJob, $supLoc, $empNum, $payDate, $msg);
	} else {
		draw_job_list_page ( $supNum, $supName, $supJob, $supLoc, $empNum, $empName, $payDate, $job_ref);
	}	
} elsif ( $head_count == 0 ) {
	my $msg = "No Employee Found for # $empNum";
	redraw_task_job_screen ($supNum, $supName, $supJob, $supLoc, $empNum, $payDate, $msg);
} else {
	my $msg = "MORE THAN 1 Employee Found for # $empNum!!!";
	redraw_task_job_screen ($supNum, $supName, $supJob, $supLoc, $empNum, $payDate, $msg);
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################



sub draw_job_list_page
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob = $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $empName = $_[5];
	my $payDate = $_[6];
	my $job_ref = $_[7];
	my @jLst = @$job_ref;
	my $jCount = $#jLst;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_03_sup_task_job_data</title>
	</head>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3>Modify Tasks - $empName $payDate </h3>
	<div id='box1'>
	Supervisor Number:<br/><br/>
	Supervisor Name:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_04_sup_adjust_task_data.pl' method='post'>
	<input type="hidden" name=supNum value='$supNum'/>
	<input type="hidden" name=supName value='$supName'/>
	<input type="hidden" name=supJob value='$supJob'/>
	<input type="hidden" name=supLoc value='$supLoc'/>
	<input type="hidden" name=empNum value='$empNum'/>
	<input type="hidden" name=empName value='$empName'/>
	<input type="hidden" name=payDate value='$payDate'/>
	<span>$supNum</span><br/><br/>
	<span>$supName</span></b><br/><br/>
	<p>Select by Client and Document<p/>
	<select style="width:200px" id=jobID name="jobID" tabindex=1 >
ENDHEADER
	for (my $i=0,; $i<=$#jLst; $i++) {
		my $job_num =  $jLst[$i][0];
		my $doc_num = $jLst[$i][1];
		my $client_id = $jLst[$i][2];
		my $str = $client_id . " " . $doc_num;
   		print "<option value=$job_num >$str</option>";
	}

print <<ENDHEADER;
</select>
<br/><br/><br/><br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a>
<br/><br/>
</form>
</body>
</html>
ENDHEADER

}

sub redraw_task_job_screen 
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob =  $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $jobDate = $_[5];
	my $msg = $_[6];
	my $date;
 	my @payDates=();		
	my $payDates_ref = get_payroll_dates_incl_today;
   @payDates = @$payDates_ref;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body onload='startUp1()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3>Modify Tasks for Employee</h3>
	<div id='box1'>
	Supervisor Number:<br/><br/>
	Supervisor Name:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_sup_task_job_data.pl' method='post'>
	<input type="hidden" name=supNum value='$supNum'/>
	<input type="hidden" name=supName value='$supName'/>
	<input type="hidden" name=supJob value='$supJob'/>
	<input type="hidden" name=supLoc value='$supLoc'/>
	<span>$supNum</span><br/><br/>
	<span>$supName</span></b><br/><br/>
	<table cellspacing = "4" cellpadding="6">
		<tr>
			<td>Employee Number: &nbsp</td>
			<td><input type="text" size=5 name="empNum" value = $empNum /></td>
		</tr>
		<tr>
			<td>Select Date:</td>
			<td>
				<select style="width:120px" id=payDate name="payDate" tabindex=1 >
ENDHEADER
			foreach $date (@payDates) {
   			print "<option value=$date >$date</option>";
			}
print <<ENDHEADER;
			</td>
	</table>
<br/><br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_03_get_employee_list.pl?loc=$supLoc" target="_blank">Get Employee List<a/>
<br/><br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a><br/><br/>
<b id="warn">$msg</b>
</form>
</body>
</html>

ENDHEADER
}


sub draw_dirty_time_card_page
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob = $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $empName = $_[5];
	my $empFac = $_[6];
	my $payDate = $_[7];
	my $jobCount = $_[8];
	my @tcData = ();
	my $tcDataRef = get_time_card_data ($supNum);
	@tcData = @$tcDataRef;
	my $client_ref = get_time_card_clients ($empFac); 
	my @clients = @$client_ref;
  print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_time_card_data</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Time Card: $empName : $payDate : Facility # $empFac</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_04_sup_time_card_refresh.pl' method='post'>
  <input type="hidden" name="supNum" value='$supNum'/>
  <input type="hidden" name="supName" value='$supName'/>
  <input type="hidden" name="supJob" value='$supName'/>
  <input type="hidden" name="supLoc" value='$supLoc'/>
  <input type="hidden" name="empNum" value='$empNum'/>
  <input type="hidden" name="empName" value='$empName'/>
  <input type="hidden" name="empFac" value='$empFac'/>
  <input type="hidden" name="payDate" value='$payDate'/>
  <input type="hidden" name="cleanDirty" value='DIRTY'/>
  
  <table id="small">
	<thead>
    <tr>
      <th>Time</th>
      <th>Doc No. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
      <th>Client</th>
    </tr>
    </thead>
	 <tbody>	
ENDHEADER
# <tbody class="scrollContent">	
 
 # print "THERE ARE $#tasks TASKS!<br/>"; 
 for my $i (0..$#tcData ) {
		my $id = $tcData[$i][0];		
		my $tag = $tcData[$i][1];	
		my $date = $tcData[$i][2];
		my $num = $tcData[$i][3];
		my $start = $tcData[$i][4];
		my $end = $tcData[$i][5];
		my $doc = $tcData[$i][6];
		my $cli = $tcData[$i][7];
		my $ti = $i;
#		if ( $cli && $cli ne "") {
#			print "$id, $tag, $date, $num, $start, $end, $doc, $cli <br/>";
#		}
		print <<ENDHEADER;
		<tr>
			<td align=left>$tag &nbsp&nbsp</td>
ENDHEADER
			if ($doc && $doc ne "" ) {
				print "<td align=left><input type='text' size='9' value='$doc' name='DOC_$id' /></td>";
			} else { 
				print "<td align=left><input type='text' size='9' name='DOC_$id' /></td>";
			}
			print "<td> <select id=clientDrop name='CLI_$id' tabindex='2' >";
			# add a blank element at the start of the list
			print "<option value='NUL'></option>";
			# add all clients on the list to the drop down
			foreach my $Cli (@clients) {
				if ( $cli && $cli ne "" && $Cli eq $cli ) {
		  			print "<option selected = 'selected' value=$Cli >$Cli</option>";
		  		} else {
		  			print "<option value=$Cli >$Cli</option>";
		  		}	
			}
			print <<ENDHEADER;
			</td>
		</tr>
ENDHEADER
 }
 print <<ENDHEADER;
 </tbody>
 </table>

 <br/><br/>
 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
 <p><input type='submit' value='Refresh Time Card Page'></p>
 <br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a>
 </form>
 </body>
  </div>	
 </html>
ENDHEADER

}



sub draw_clean_time_card_page
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob = $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $empName = $_[5];
	my $empFac = $_[6];
	my $payDate = $_[7];
	my $jobCount = $_[8];
	my @tcData = ();
	my $tcData = get_time_card_data ($supNum);
	@tcData = @$tcData;
	my $client_ref = get_time_card_clients ($empFac); 
	my @clients = @$client_ref;

	# add a blank element at the start of the list
# 	my $optStr = "\<option value='NUL'\>\<\/option\>";
#	print "<option value='NUL'></option>";
	# add all clients on the list to the drop down
#	foreach my $cli (@clients) {
 #  	$optStr = $optStr . "\<option value\=$cli \>$cli\<\/option\>";
#	}
#	print "here is the option list $optStr<br/>";


  print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_time_card_data</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Time Card: $empName : $payDate : Facility # $empFac</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_04_sup_time_card_refresh.pl' method='post'>
  <input type="hidden" name="supNum" value='$supNum'/>
  <input type="hidden" name="supName" value='$supName'/>
  <input type="hidden" name="supJob" value='$supJob'/>
  <input type="hidden" name="supLoc" value='$supLoc'/>
  <input type="hidden" name="empNum" value='$empNum'/>
  <input type="hidden" name="empName" value='$empName'/>
  <input type="hidden" name="empFac" value='$empFac'/>
  <input type="hidden" name="payDate" value='$payDate'/>
  <input type="hidden" name="cleanDirty" value='CLEAN'/>
  <table id="small">
	<thead>
    <tr>
      <th>Time</th>
      <th>Doc No. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
      <th>Client</th>
    </tr>
    </thead>
	 <tbody>	
ENDHEADER
# <tbody class="scrollContent">	
 
 # print "THERE ARE $#tasks TASKS!<br/>"; 
 for my $i (0..$#tcData ) {
		my $id = $tcData[$i][0];		
		my $tag = $tcData[$i][1];	
		my $date = $tcData[$i][2];
		my $num = $tcData[$i][3];
		my $start = $tcData[$i][4];
		my $end = $tcData[$i][5];
		my $cli = $tcData[$i][6];
		my $ti = $i;
		if ( $cli eq "" ) {
#			print "$id, $tag, $date, $num, $start, $end, $cli <br/>";
			print <<ENDHEADER;
			<tr>
				<td align=left>$tag &nbsp&nbsp</td>
				<td align=left><input type="text" size="9" name="DOC_$id" /></td>
				<td> <select id=clientDrop name="CLI_$id" tabindex="2" >
ENDHEADER
#			print "$optStr";
			# add a blank element at the start of the list
			print "<option value='NUL'></option>";
			# add all clients on the list to the drop down
			foreach my $cli (@clients) {
	  			print "<option value=$cli >$cli</option>";
			}
			print <<ENDHEADER;
				</td>
			</tr>
ENDHEADER
		} else {
			print <<ENDHEADER;
			<tr>
				<td align=left>$tag &nbsp&nbsp</td>
				<td id="info" >$cli</td>
				<td><input type="hidden" name="DOC_$id" value="-1" /><input type="hidden" name="CLI_$id" value="logged_in" /></td> 
			</tr>
ENDHEADER
#			print "$id, $tag, $date, $num, $start, $end, $cli <br/>";
#			<td><input type='text' value='$cli' size='12' name='new_std_${id}' disabled="disabled" /></td>

		}	
			
 }
 print <<ENDHEADER;
 </tbody>
 </table>

 <br/><br/>
 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
 <p><input type='submit' value='Refresh Time Card Page'></p>
 <br/>
 <p><a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a><br/></p>
<br/><br/>
 </form>
 </body>
  </div>	
 </html>
ENDHEADER

}

